package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("order")
@Slf4j
public class ordersController {
    @Autowired
    private OrdersService  ordersService;

    @Autowired
    private OrderDetailService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;
    //用户端历史订单
//    @GetMapping("userPage")
//    public R<Page> userPage( Integer page,Integer pageSize){
//        //创建分页查询构造器
//        Page<Orders> ordersPage = new Page<>(page,pageSize);
//        //创建条件构造器
//        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
//        lqw.orderByDesc(Orders::getCheckoutTime);
//        //创建新的分页构造器
//        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
//        ordersService.page(ordersPage,lqw);
//        //进行拷贝
//        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
//        List<OrdersDto> records = ordersDtoPage.getRecords();
//        List<OrdersDto> dtoList = records.stream().map((item) -> {
//            OrdersDto ordersDto = new OrdersDto();
//            BeanUtils.copyProperties(item,ordersDto);
//            Long id = item.getId();
//            Orders orders = ordersService.getById(id);
//            ordersDto.setUserName(orders.getUserName());
//            ordersDto.setPhone(orders.getPhone());
//            ordersDto.setAddress(orders.getAddress());
//            ordersDto.setConsignee(orders.getConsignee());
//            return ordersDto;
//        }).collect(Collectors.toList());
//        ordersDtoPage.setRecords(dtoList);
//        return R.success(ordersDtoPage);
//    }

    //下单功能
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }


    //用户历史订单
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> orderDetailR(HttpSession session, Integer page, Integer pageSize)
    {
        log.info(page+"+"+pageSize);

//        String phone = session.getAttribute("phone")+"";

        Long currentId = BaseContext.getCurrentId();
        log.info("ID:"+currentId);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId,currentId);
        Page<Orders> page1 = new Page<>(page,pageSize);
        ordersService.page(page1,lambdaQueryWrapper);
//        List<Orders> records = page1.getRecords();
//        records.forEach((i)->{
//            LambdaQueryWrapper<User> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
//
//            LambdaQueryWrapper<AddressBook> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper2.eq(AddressBook::getUserId,currentId);
//            lambdaQueryWrapper1.eq(User::getId,currentId);
//            User one = userService.getOne(lambdaQueryWrapper1);
//
//            String name = one.getName();
//            i.setUserName(name);
//            AddressBook one1 = addressBookService.getOne(lambdaQueryWrapper2);
//
//            String phone = one1.getPhone();
//            String detail = one1.getDetail();
//            String consignee = one1.getConsignee();
//            i.setAddress(detail);
//            i.setConsignee(consignee);
//            i.setPhone(phone);
//        });
//        page1.setRecords(records);
        Page<OrdersDto> page2 = new Page<>();
        BeanUtils.copyProperties(page1,page2,"records");

//        List<OrdersDto> records1 = page2.getRecords();

        List<OrdersDto> collect = page1.getRecords().stream().map(i ->
        {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(i, ordersDto);
            LambdaQueryWrapper<OrderDetail> Detail = new LambdaQueryWrapper<>();
            Detail.eq(OrderDetail::getOrderId, i.getId());

            List<OrderDetail> list = orderService.list(Detail);
            ordersDto.setOrderDetails(list);
            return ordersDto;
        }).collect(Collectors.toList());

        page2.setRecords(collect);
        log.info(page2.getRecords().size()+"");
        return R.success(page2);
    }

    //商家端订单明细
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
        //创建分页查询构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        if (number != null){
            lqw.eq(Orders::getNumber,number);
        }
        if (beginTime != null && endTime != null){
           // lqw.between(Orders::getOrderTime,beginTime,endTime);
            lqw.gt(Orders::getOrderTime,beginTime);
            lqw.lt(Orders::getOrderTime,endTime);
        }
        lqw.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage, lqw);
        return R.success(ordersPage);
    }
    //状态修改
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        //创建条件构造器
        LambdaQueryWrapper<Orders> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId,orders.getId());
        lqw.eq(Orders::getStatus,2);
        int count = ordersService.count(lqw);
        if (count > 0){
            orders.setStatus(3);
            ordersService.updateById(orders);
        }
       return R.success("修改成功");
    }
}
